package com.example.administrator.compileapp;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.compileapp.activity.OtherButtonActivity;
import com.example.administrator.compileapp.activity.OtherStartActivity;
import com.example.administrator.compileapp.activity.OtherStartListActivity;
import com.example.administrator.compileapp.activity.StartActivity;
import com.example.administrator.compileapp.databinding.ActivityMainBinding;
import com.example.administrator.compileapp.drawer.LoginActivity;

import cn.sharesdk.onekeyshare.OnekeyShare;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   private ActivityMainBinding  binding;
    private FragmentTransaction transaction;
    private FragmentManager manager;
    private boolean isExit = false;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };
    private String figureurl_qq_1;
    private String nickName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
       figureurl_qq_1 = getIntent().getStringExtra("figureurl_qq_1");
        nickName =getIntent().getStringExtra("nickName");
        aboutListener();
        aboutFragmentTransaction();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK){
            exit();
            return  false ;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void exit(){
        if(!isExit){
            isExit = true;
            Toast.makeText(MainActivity.this, "再按一次退出编译原理学习平台", Toast.LENGTH_SHORT).show();
            handler.sendEmptyMessageDelayed(0,2000);
        }else{
            finish();
            System.exit(0);
        }
    }
    private void aboutFragmentTransaction() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        DrawerFragment drawerFragment = DrawerFragment.newInstance(nickName,figureurl_qq_1);
        transaction.add(R.id.main_drawer,drawerFragment);
        transaction.commit();
    }

    private void aboutListener() {
        binding.start.setOnClickListener(this);
        binding.teacher.setOnClickListener(this);
        binding.studyAim.setOnClickListener(this);
        binding.studyHelp.setOnClickListener(this);
        binding.class1.setOnClickListener(this);
        binding.back.setOnClickListener(this);
        binding.otherStart.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.teacher:
                aboutIntentOtherButton("教师简介");
                break;
            case R.id.study_aim:
                aboutIntentOtherButton("学习目标");
                break;
            case R.id.study_help:
                aboutIntentOtherButton("学习帮助");
                break;
            case R.id.class1:
                aboutIntentOtherButton("课程简介");
                break;
            case R.id.start:
                Intent intent4 = new Intent(this,StartActivity.class);
                startActivity(intent4);
                break;
            case R.id.other_start:
                Intent intent5 = new Intent(this, OtherStartListActivity.class);
                startActivity(intent5);
                break;
            case R.id.back:
                binding.activityMain.openDrawer(Gravity.LEFT);
                break;
        }
    }

    private void aboutIntentOtherButton(String title) {
        Intent intent = new Intent(this,OtherButtonActivity.class);
        intent.putExtra("title",title);
        startActivity(intent);
    }
}
